//管理员修改评价

import React, { useEffect, useState } from 'react';
import { Layout, Form, Input, Button, Table,Modal, message,Space, Card } from 'antd';
import { fetchSearchUserInfo } from '@/store/modules/adminInf';
import { useDispatch, useSelector } from 'react-redux';
import { getEvaluateAPI,getMockExaminationEvaluateAPI,updateEvaluateAPI, updateMockExaminationEvaluateAPI } from '@/api/admin/evaluate';
const { Search } = Input;

const { Header, Content, Footer } = Layout;

const Business = () => {
  const dispatch =useDispatch()
  const [form] = Form.useForm();
  const [searchData,setSearchData] =useState([])
  const [courseModalVisible, setCourseModalVisible] = useState(false);
  const [mockModalVisible, setMockModalVisible] = useState(false);

  //课程留言表头

  const columns = [
    {
      title: '评价ID',
      dataIndex: 'evaluateId',
      key: 'evaluateId',
    },
    {
      title: '业务名称',
      dataIndex: 'businessName',
      key: 'businessName',
    },
    {
      title: '学员评价内容',
      dataIndex: 'studentEvaluateContent',
      key: 'studentEvaluateContent',
    },
    {
      title: '教练评价内容',
      dataIndex: 'coachEvaluateContent',
      key: 'coachEvaluateContent',
    },
    {
      title: '评价时间',
      dataIndex: 'evaluateTime',
      key: 'evaluateTime',
    },
    {
      title: '操作',
      key: 'action',
      render: (text, record) => (
        <Space size="middle">
          <Button onClick={() => handleEdit(record)}>编辑</Button>
          {/* <Button onClick={() => handleDelete(record.studentId)}>删除</Button> */}
        </Space>
      ),
    },
  ];
  //课程留言表头

  const mockColumns = [
    {
      title: '模拟考试ID',
      dataIndex: 'mockExaminationId',
      key: 'mockExaminationId',
    },
    {
      title: '模拟考试日期',
      dataIndex: 'mockExaminationDate',
      key: 'mockExaminationDate',
    },
    {
      title: '模拟考试名称',
      dataIndex: 'mockExaminationName',
      key: 'mockExaminationName',
    },
    {
      title: '模拟考试分数',
      dataIndex: 'mockExaminationScore',
      key: 'mockExaminationScore',
    },
    {
      title: '学员姓名',
      dataIndex: 'mockStudentName',
      key: 'mockStudentName',
    },
    {
      title: '教练姓名',
      dataIndex: 'mockExaminationCoachName',
      key: 'mockExaminationCoachName',
    },
    {
      title: '评语',
      dataIndex: 'mockExaminationMessage',
      key: 'mockExaminationMessage',
    },
    {
      title: '操作',
      key: 'action',
      render: (text, record) => (
        <Space size="middle">
          <Button onClick={() => handleMockEdit(record)}>编辑</Button>
          {/* <Button onClick={() => handleDelete(record.studentId)}>删除</Button> */}
        </Space>
      ),
    },
  ];

  const handleEdit = (record) => {
    form.setFieldsValue(record);
    setCourseModalVisible(true);
  };

  //修改评论
  const handleSave = () => {
      form.validateFields()
      .then(values =>{
        console.log(values)
          updateEvaluateAPI(values.evaluateId,values.studentEvaluateContent,values.coachEvaluateContent)
            message.success('修改成功！')
            form.resetFields();//清空内容
            setCourseModalVisible(false); 
        })
        .catch(error => console.error(error));
        
    }

//处理模拟考试表格
    const handleMockEdit = (record) => {
      form.setFieldsValue(record);
      setMockModalVisible(true);
    };
  
    //修改评论
    const handleMockEditSave = () => {
        form.validateFields()
        .then(values =>{
          console.log(values)
            updateMockExaminationEvaluateAPI(values.mockExaminationId,values.mockExaminationScore,values.mockExaminationMessage)
              message.success('修改成功！')
              form.resetFields();//清空内容
              setMockModalVisible(false);
          })
          .catch(error => console.error(error));
          
      }

  const [evaluateData,setEvaluateData] =useState([])
  const [mockExamEvaluateData,setMockExamEvaluateData] =useState([])

  //获取评价列表
  useEffect(()=>{
    async function getEvaluateData(){
      //获取课程留言列表
      const res= await getEvaluateAPI()
      //获取模拟考试列表
      const resData = await getMockExaminationEvaluateAPI()
      setEvaluateData(res.data)
      setMockExamEvaluateData(resData.data)
    }
    getEvaluateData()
  },[])

  return (
    <Layout>
      <Header style={{ color: 'white' }}>评价管理</Header>
      <Content style={{ padding: '10 50px' }}>
        <div style={{ background: '#fff', padding: 24, minHeight: 280 }}>
        <h1>课程留言</h1>
          <Table columns={columns} dataSource={evaluateData} />
        </div>
        <Modal
          title="编辑留言信息"
          visible={courseModalVisible}
          cancelText='取消'
          okText='提交'
          onCancel={() => setCourseModalVisible(false)}
          onOk={handleSave}
        >
          <Form form={form} layout="vertical" initialValues={{}}>
            <Form.Item name="key" hidden><Input /></Form.Item>
            <Form.Item name="evaluateId" hidden><Input /></Form.Item>
            <Form.Item label="学员评价内容" name="studentEvaluateContent">
              <Input />
            </Form.Item>
            <Form.Item label="教练评价内容" name="coachEvaluateContent" rules={[{ required: true, message: '请输入教练评价内容' }]}>
              <Input />
            </Form.Item>
          </Form>
        </Modal>
        <Card>
      <div style={{ background: '#fff', padding: 24, minHeight: 280 }}>
          <h1>模拟考试</h1>
          <Table columns={mockColumns} dataSource={mockExamEvaluateData} />
          <Modal
          title="编辑考试信息"
          visible={mockModalVisible} 
          cancelText='取消'
          okText='提交'
          onCancel={() => setMockModalVisible(false)}
          onOk={handleMockEditSave}
        >
          <Form form={form} layout="vertical" initialValues={{}}>
            <Form.Item name="key" hidden><Input /></Form.Item>
            <Form.Item name="mockExaminationId" hidden><Input /></Form.Item>
            <Form.Item label="模拟考试分数" name="mockExaminationScore">
              <Input />
            </Form.Item>
            <Form.Item label="评语" name="mockExaminationMessage">
              <Input />
            </Form.Item>
          </Form>
        </Modal>
        </div>
      </Card>
      </Content>
      <Footer>
     <div style={{position:'absolute',left:'50%'}}>驾校管理系统&copy;2024-2099</div>
   </Footer>
    </Layout>
  );
};

export default Business;